﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports Microsoft.TeamFoundation.Client

Public Class TFSImport

    Private m_ItemWrapperCollection As ItemWrapperCollection

    Public Sub Import(ByRef ItemWrapperCollection As ItemWrapperCollection, ByVal TreeType As Node.TreeType)
        ' Clear current content.
        ItemWrapperCollection.Clear()
        'Try
        ' Store localy for ease of access
        m_ItemWrapperCollection = ItemWrapperCollection
        ' Do import
        Dim NodeCol As NodeCollection = Nothing
        Select Case TreeType
            Case Node.TreeType.Area
                NodeCol = Application.SelectedProject.AreaRootNodes
            Case Node.TreeType.Iteration
                NodeCol = Application.SelectedProject.IterationRootNodes
        End Select
        ImportNodes(NodeCol)
        ' Item added to the collection as the primary-Item.
        If m_ItemWrapperCollection.Count > 0 Then
            m_ItemWrapperCollection.Current = m_ItemWrapperCollection(0)
        End If
        'Catch ex As Exception
        '    MsgBox(ex.ToString)
        'End Try
    End Sub

    Private Sub ImportNodes(ByRef Nodes As NodeCollection)
        ' Add parent Nodes
        For Each n As Node In Nodes
            m_ItemWrapperCollection.Add(New ItemWrapper(n))
            Dim added As ItemWrapper = m_ItemWrapperCollection.Find(CStr(n.Id))
            ImportChildNodes(n, added)
        Next

        ' Add top level nodes as siblings
        For Each tn As Node In Nodes
            For Each n As Node In Nodes
                If Not n Is tn Then
                    m_ItemWrapperCollection.AddSibling(m_ItemWrapperCollection.Find(CStr(tn.Id)), m_ItemWrapperCollection.Find(CStr(n.Id)))
                End If
            Next
        Next

    End Sub

    Private Sub ImportChildNodes(ByRef Node As Node, ByVal iw As ItemWrapper)
        For Each n As Node In Node.ChildNodes
            m_ItemWrapperCollection.Add(New ItemWrapper(n))
            Dim added As ItemWrapper = m_ItemWrapperCollection.Find(CStr(n.Id))
            m_ItemWrapperCollection.AddChild(iw, added)
            ImportChildNodes(n, added)
        Next
    End Sub

    Public Shared Sub ImportIterations()

    End Sub

End Class
